
package com.example.moshudriver.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.BeeFramework.activity.BaseActivity;
import com.BeeFramework.model.BusinessResponse;
import com.BeeFramework.view.ToastView;
import com.sgr.dykdriver.R;
import com.example.moshudriver.model.UserModel;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.protocol.USER;
import com.example.moshudriver.tools.O2OMobileAppConst;
import com.external.androidquery.callback.AjaxStatus;
//个人签名
public class C12_EditSignatureActivity extends BaseActivity implements BusinessResponse, View.OnClickListener {
    private TextView mTitle;
    private ImageView mBack;
//    private ImageView mTopViewRightImage;
    private Button mSave;
    private SharedPreferences mShared;
    private EditText mSignture;
    private UserModel mUserModel;
    private TextView mTextNum;
    private int mTotalSize = 20; //g签名�?�?20个字

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.c12_edit_signature);
        mTitle = (TextView) findViewById(R.id.top_view_title);
        mTitle.setText(getString(R.string.edit_signature));
        mBack = (ImageView) findViewById(R.id.top_view_back);
        mSignture = (EditText) findViewById(R.id.et_signature);
        mTextNum = (TextView) findViewById(R.id.text_num);
        mSave = (Button) findViewById(R.id.btn_save);
//        mTopViewRightImage = (ImageView) findViewById(R.id.top_view_right_image);
//        mTopViewRightImage.setVisibility(View.INVISIBLE);
        mShared = getSharedPreferences(O2OMobileAppConst.USERINFO, 0);
        String userStr = mShared.getString("user", "");
        try {
            if (userStr != null) {
                JSONObject userJson = new JSONObject(userStr);
                USER user = new USER();
                user.fromJson(userJson);
                mSignture.setText(user.signature);
                mTextNum.setText(mTotalSize - mSignture.getText().length() + "");
                mSignture.setSelection(user.signature.length());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mUserModel = new UserModel(this);
        mUserModel.addResponseListener(this);
        mSave = (Button) findViewById(R.id.btn_save);
        mBack.setOnClickListener(this);
        mSave.setOnClickListener(this);
        //监听字数变化
        mSignture.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mTextNum.setText(mTotalSize - mSignture.getText().length() + "");

            }
        });
    }

    @Override
    public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status) throws JSONException {
        if(url.endsWith(ApiInterface.USER_CHANGE_PROFILE)){
            ToastView toast = new ToastView(C12_EditSignatureActivity.this, getString(R.string.signature_success));
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            finish();

        }
    }

    @Override
    public void onClick(View v) {
        String signture = mSignture.getText().toString().trim();
        switch (v.getId()) {
            case R.id.top_view_back:
                finish();
                break;
            case R.id.btn_save:
                if ("".equals(signture)) {
                    ToastView toast = new ToastView(C12_EditSignatureActivity.this, getString(R.string.signature_not_null));
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                    mSignture.setText("");
                    mSignture.requestFocus();
                    break;
                }else{
                    mUserModel.changeSignature(signture);
                }
        }
    }
}